package com.huawei.fastengine.fastview.download.install;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.IPackageInstallObserver;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build.VERSION;
import android.util.Log;
import com.huawei.fastengine.fastview.download.utils.PackageManagerUtils;
import java.io.File;
import java.lang.reflect.InvocationTargetException;

public abstract class InstallProcess {
    private static final String TAG = "InstallProcess";

    public static Intent getNomalInstallIntent(Context context, String str) {
        Intent intent = new Intent();
        if (!(context instanceof Activity)) {
            intent.setFlags(268435456);
        }
        intent.setAction("android.intent.action.INSTALL_PACKAGE");
        intent.setData(Uri.fromFile(new File(str)));
        return intent;
    }

    private static boolean innerInstall(Context context, ManagerTask managerTask) {
        Log.i(PackageManagerConstants.TAG, " innerInstall begin:");
        if (installApk(context, managerTask)) {
            return true;
        }
        Log.e(PackageManagerConstants.TAG, " inner install failed!!!!");
        return false;
    }

    private static boolean innerInstall(Context context, ManagerTask managerTask, int i, PackageManager packageManager) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        if (VERSION.SDK_INT >= 24) {
            int installPackage = InstallProcessSdk24.installPackage(context, managerTask.getPackageName(), managerTask.getPath());
            if (-1 == installPackage) {
                Log.e(TAG, " can not inner install!pkg:");
                return true;
            } else if (installPackage == 0) {
                Log.e(TAG, "sdk24 install failed,IOExecption.");
            }
        } else {
            Uri fromFile = Uri.fromFile(new File(managerTask.getPath()));
            PackageManager.class.getMethod("installPackage", new Class[]{Uri.class, IPackageInstallObserver.class, Integer.TYPE, String.class}).invoke(packageManager, new Object[]{fromFile, new PackageInstallObserver(managerTask), Integer.valueOf(i), context.getPackageName()});
        }
        return false;
    }

    private static boolean installApk(Context context, ManagerTask managerTask) {
        PackageManager packageManager = context.getPackageManager();
        try {
            if (PackageManagerUtils.systemApp(context) && innerInstall(context, managerTask, 2, packageManager)) {
                return false;
            }
            Log.i(TAG, " inner install end!");
            return true;
        } catch (IllegalArgumentException e) {
            Log.e(TAG, " installPackage IllegalArgumentException ");
            return false;
        } catch (Throwable th) {
            Log.e(TAG, " inner install exception: ");
            return false;
        }
    }

    public static void installProcess(Context context, ManagerTask managerTask) {
        Log.d(TAG, "install installProcess: start" + PackageManagerUtils.systemApp(context));
        Log.d(TAG, "install installProcess: Build.VERSION.SDK_INT: " + VERSION.SDK_INT);
        if (PackageManagerUtils.systemApp(context)) {
            boolean innerInstall = innerInstall(context, managerTask);
            Log.d(TAG, "install systemApp innerResult: " + innerInstall);
            if (!innerInstall) {
                systemInstall(context, managerTask);
                return;
            }
            return;
        }
        systemInstall(context, managerTask);
    }

    private static void systemInstall(Context context, ManagerTask managerTask) {
        Log.i(TAG, " system install:" + managerTask.getPackageName());
        File file = new File(managerTask.getPath());
        if (file.exists() && file.isFile() && file.length() > 0) {
            Intent intent = new Intent(context, PackageInstallerActivity.class);
            intent.putExtra(PackageInstallerActivity.INSTALL_PATH, managerTask.getPath());
            intent.putExtra(PackageInstallerActivity.INSTALL_PACKAGENAME, managerTask.getPackageName());
            intent.putExtra(PackageInstallerActivity.INSTALL_EXISTING, false);
            intent.putExtra(PackageInstallerActivity.INSTALL_CHANGE_PATH_TIMES, 0);
            intent.setFlags(402653184);
            try {
                context.startActivity(intent);
                return;
            } catch (ActivityNotFoundException e) {
                Log.e(TAG, " can not start install !");
                return;
            }
        }
        Log.e(TAG, " system install failed,file not existed filePath:");
    }
}
